//
//  DetailedViewController.m
//  Leisure
//
//  Created by xalo on 16/6/29.
//  Copyright © 2016年 蓝鸥科技有限公司西安分公司. All rights reserved.
//

#import "DetailedViewController.h"

@interface DetailedViewController ()<UIWebViewDelegate>
@property (weak, nonatomic) IBOutlet UILabel *titleLabel;
@property (weak, nonatomic) IBOutlet UIImageView *middleImageView;
@property (weak, nonatomic) IBOutlet UITextView *textView;


@property (nonatomic,strong)UIWebView *webView;

@end

@implementation DetailedViewController

-(UIWebView *)webView{
    if (!_webView) {
        _webView = [[UIWebView alloc]initWithFrame:self.view.frame];
        _webView.delegate = self;
        _webView.scrollView.bounces = NO;
        _webView.scrollView.showsHorizontalScrollIndicator = NO;
        _webView.scrollView.showsVerticalScrollIndicator = NO;
    }
    return _webView;
}


- (void)viewDidLoad {
    [super viewDidLoad];
    [self.view addSubview:self.webView];
    
    //请求数据
    [RequestManager fetchDataWithUrl:kReadListDetailedURL parameter:@{@"contentid":self.contentID} method:@"POST" completed:^(NSData *data) {
        
        NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:nil];
//        NSLog(@"%@",dic[@"data"][@"html"]);
        //根据<pre>分割
        NSArray *array = [dic[@"data"][@"html"] componentsSeparatedByString:@"<pre>"];
        [self.webView loadHTMLString:array[0] baseURL:nil];

    } updateUI:^{

    }];
    
    

}
#pragma mark ----UIWebViewDelegate----
-(void)webViewDidFinishLoad:(UIWebView *)webView{
    NSLog(@"完成加载");
    NSString *js = @"function imgAutoFit() { \
    var imgs = document.getElementsByTagName('img'); \
    for (var i = 0; i < imgs.length; ++i) {\
    var img = imgs[i];   \
    img.style.maxWidth = %f;   \
    } \
    }";
    js = [NSString stringWithFormat:js, [UIScreen mainScreen].bounds.size.width - 20];
    
    [webView stringByEvaluatingJavaScriptFromString:js];
    [webView stringByEvaluatingJavaScriptFromString:@"imgAutoFit()"];
    
    
}
-(void)webViewDidStartLoad:(UIWebView *)webView{
    NSLog(@"开始加载");

}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
